// 提示信息语言
var tipsLang = {
    mobileErr: "请输入正确手机号",
    mobileMiss: "手机号未输入",
    mobileBuzz: "该手机接收短信请求过于频繁，请稍后再试",
    mobileBound: "该手机号已被绑定过，请尝试其他号码",
    codeErr: "请输入正确验证码",
    codeMiss: "验证码未输入",
    pinErr: "请输入正确短信验证码",
    pinMiss: "短信验证码未输入",
    accountErr: "请输入正确手机账号 / 个性账号",
    accountMiss: "手机账号 / 个性账号未输入",
    pwdErr: "密码输入不正确",
    pwdMiss: "密码未输入",
    amountErr: "充值金额为 1 - 100000 整数",
    amountMiss: "充值金额未输入",
    ocnumErr: "请输入 16 位一卡通卡号",
    ocnumMiss: "一卡通卡号未输入",
    ocpwdErr: "请输入 8 位一卡通密码",
    ocpwdMiss: "一卡通密码未输入",
    fillnumErr: "请输入 15 - 20 位充值卡卡号",
    fillnumMiss: "充值卡卡号未输入",
    fillpwdErr: "请输入 8 - 64 位充值卡密码",
    fillpwdMiss: "充值卡密码未输入",
    formhasErr: "提交的信息有错误，请确认您的输入"
};

// 手机号
var isMobile = /^(?=[\d ]{13}$)^1(?:3\d|4[57]|5[^4\D]|7[^249\D]|8\d)[\d ]{10}$/;

// 密码6-16位，区分大小
var isPwd = /^.{6,16}$/;

// 一卡通卡号
var isOcnum = /^[\d ]{19}$/;

// 一卡通密码
var isOcpwd = /^[\d ]{9}$/;

// 充值卡卡号
var isFillnum = /^[\d ]{18,24}$/;

// 充值卡密码
var isFillpwd = /^[\d ]{10,79}$/;

// 充值金额 1 - 100000
var isAmount = /^([1-9]|[1-9][0-9]{1,4}|100000)$/;

// 验证码
var isImgCode = /^[0-9a-zA-Z]{4}$/;

// 手机/个性账号
var isAccount = /^[\S]{1,50}$/;

// 输入数字过滤
String.prototype.filterNum = function(el) {
    el.value = el.value.replace(/\D*$/g, "")
        .slice(0, el.maxLength);
    return el;
}

//去掉所有空格
String.prototype.trim = function() {
    return this.replace(/\s+/g, "");
};

// 检测 IE 版本
var isIE = function(ver) {
    var b = document.createElement('b')
    b.innerHTML = '<!--[if IE ' + ver + ']><i></i><![endif]-->'
    return b.getElementsByTagName('i').length === 1
}

// 检测表单元素输入值
var formCheck = function(el, valRegex, tipsErr, tipsMiss) {
    var val = $.trim(el.val());
    var isError = !valRegex.test(val);
    var closest = el.closest(".label-item");
    var panel = el.closest(".pay-panel");
    var btnOption = panel.find(".label-option");
    closest.find(".form-tips").remove();
    if (isError) {
        el.parent().addClass("has-error");
        $("<p class='form-tips'><i class='zticon'>&#xe622;</i>" + tipsErr + "</p>").appendTo(closest);
        // 输入为空时提示
        if (val == "") {
            closest.find(".form-tips").remove();
            $("<p class='form-tips'><i class='zticon'>&#xe622;</i>" + tipsMiss + "</p>").appendTo(closest);
        }
        return false;
    } else {
        el.parent().removeClass("has-error");
        return true;
    }
}

// 检测手机号
var checkMobile = function(el) {
    return formCheck(el, isMobileNum, tipsLang.mobileErr, tipsLang.mobileMiss)
};

// 检测个性账号
var checkAccount = function(el) {
    return formCheck(el, isAccount, tipsLang.accountErr, tipsLang.accountMiss)
};

// 检测输入金额
var checkAmount = function(el) {
    return formCheck(el, isAmount, tipsLang.amountErr, tipsLang.amountMiss)
};

// 检测手机号
var checkMobile = function(el) {
    return formCheck(el, isMobile, tipsLang.mobileErr, tipsLang.mobileMiss)
};

// 检测一卡通卡号
var checkOcnum = function(el) {
    return formCheck(el, isOcnum, tipsLang.ocnumErr, tipsLang.ocnumMiss)
};

// 检测一卡通密码
var checkOcpwd = function(el) {
    return formCheck(el, isOcpwd, tipsLang.ocpwdErr, tipsLang.ocpwdMiss)
};

// 检测一充值卡卡号
var checkFillnum = function(el) {
    return formCheck(el, isFillnum, tipsLang.fillnumErr, tipsLang.fillnumMiss)
};

// 检测一充值卡密码
var checkFillpwd = function(el) {
    return formCheck(el, isFillpwd, tipsLang.fillpwdErr, tipsLang.fillpwdMiss)
};

// 检测图形验证码
var checkCaptcha = function(el) {
    return formCheck(el, isImgCode, tipsLang.codeErr, tipsLang.codeMiss)
};

// 弹层居中显示
var mask = $("#mask");

function goCenter(el) {
    mask.show();
    var elTop = Math.max(0, (($(window).height() - el.outerHeight()) / 2));
    el.css("top", elTop);
    $("html").addClass("root-hidde");
}

// resize 弹层居中
function resizePop(el) {
    $(window).on("resize", function() {
        if (el.is(":visible")) {
            goCenter(el);
        }
    });
}

// 关闭弹层
var popClose = $(".pop-close, .popup .btn-pop a");
popClose.on("click", function() {
    $(this).closest(".popup").hide();
    $(this).closest(".popup").find(".orders").attr("orderid", "");
    mask.hide();
    $("html").removeClass("root-hidde");
});

// 提交信息出错
var optionTips = function(el, msg) {
    el.parent().addClass("btn-tips");
    el.parent().find(".form-tips").remove();
    el.after("<p class='form-tips'><i class='zticon'>&#xe622;</i>" + msg + "</p>");
};

(function($) {
    $.fn.extend({
        // 显示提交按钮信息
        addTips: function() {
            $(this).parent().addClass("btn-tips");
            $(this).parent().find(".form-tips").remove();
            $(this).after("<p class='form-tips'><i class='zticon'>&#xe622;</i>" + tipsLang.formhasErr + "</p>");
        },
        // 移除提交按钮信息
        removeTips: function() {
            $(this).parent().remove("btn-tips");
            $(this).parent().find(".form-tips").remove();
        },
        // 显示 Loading
        addLoading: function() {
            $("<em class='option-loading'><i>正在处理，请稍等</i></em>").insertAfter($(this));
        },
        // 移除 Loading
        removeLoading: function() {
            $(".option-loading").remove();
        },
        // 常见问题列表展开与伸缩
        tiplistToggle: function() {
            $("<i class='zticon'>&#xe626;</i>").prependTo($(this));
            $(this).on("click", function() {
                $(this).siblings().toggle();
                $(this).toggleClass("active");
                if ($(this).hasClass("active")) {
                    $(this).find("i").text($(this).data("active"));
                } else {
                    $(this).find("i").text($(this).data("default"));
                }
            });
        },
        // 格式化充值卡号等
        numFormat: function() {
            $(this).on("keyup", function() {
                var v = $(this).val();
                /\S{5}/.test(v) && $(this).val(v.replace(/\s/g, "").replace(/(.{4})/g, "$1 "));
            });
        },
        // 格式化手机号
        mobileFormat: function() {
            $(this).on("keyup", function() {
                var v = $(this).val().trim();
                if (!v.search(/\S|$/)) {
                    $(this).val("");
                }
                if (v != "") {
                    var length = v.length;
                    if (length <= 3) {
                        $(this).val(v.replace(/\D*$/g, ""));
                    } else {
                        if (length <= 7) {
                            $(this).val(v.replace(/\D*$/g, "").substring(0, 3) + " " + v.replace(/\D*$/g, "").substring(3, length));
                        } else {
                            $(this).val(v.replace(/\D*$/g, "").substring(0, 3) + " " + v.replace(/\D*$/g, "").substring(3, 7) + " " + v.replace(/\D*$/g, "").substring(7, length));
                        }
                    }
                }
            });
        }
    });
})(jQuery);

// 生成充值点费率表格
var CF = count_fee;
for (var i = 0; i < CF.length; i++) {
    var feetr = $("<tr id='fee" + i + "'></tr>");
    var pointhtml = (i === count_fee.length - 1) ?
        "&#165;" + CF[i].point + " 以上" :
        "&#165;" + CF[i].point + " - &#165;" + (CF[i + 1].point - 1);
    var feehtml = "1 元 = " + CF[i].fee + " 巨人点数";
    var cuthtml = Math.ceil(10000 / CF[i].fee);
    if (cuthtml === 100) {
        cuthtml = "无折扣"
    } else {
        cuthtml = "<span>" + cuthtml + " 折</span>"
    }
    var point = $("<td>", {
        html: pointhtml
    })
    var fee = $("<td>", {
        html: feehtml
    })
    var cut = $("<td>", {
        html: cuthtml
    })
    feetr.append(point, fee, cut);
    feetr.appendTo("#discount_tips tbody");
}

// 是否显示充值赠送点
var induction = $(".induction");

function payInduce(obj) {
    if (typeof count_fee === "undefined") {
        return;
    }
    var val = obj.val() || parseInt(obj.text());
    var closest = obj.closest(".pay-panel");
    for (var i in count_fee) {
        if (i === 0) {
            continue;
        }
        if (val >= (count_fee[i].point * 0.8) && val < count_fee[i].point) {
            if (parseInt(window._GPConf.rate) != 0) {
                var points = ((count_fee[i].fee - 100) * count_fee[i].point / 100) * parseInt(_GPConf.rate);
            } else {
                var points = (count_fee[i].fee - 100) * count_fee[i].point;
            }
            var induceTxt = "一次充值 " + count_fee[i].point + " 元，赠送 <strong>" + points + " </span>" + _GPConf.name;
            closest.find(induction).html(induceTxt).show();
            break;
        } else {
            closest.find(induction).hide();
        }
    }
}

// 返回优惠金额及赠送点
function discount(value, count_fee) {
    if (typeof count_fee === "undefined") {
        return;
    }
    var num = value,
        cf = count_fee,
        returnfee;
    for (var i = 0; i < cf.length; i++) {
        if (num < cf[i].point) {
            if (i === 0) {
                returnfee = cf[i].fee;
                break;
            } else {
                returnfee = cf[i - 1].fee;
                break;
            }
        }
        if (i === cf.length - 1) {
            returnfee = cf[cf.length - 1].fee;
        }
    }
    return [num * 100, num * (returnfee - 100), returnfee];
}

// 更新赠送点比率
function updateFee(obj) {
    if (typeof count_fee === "undefined") {
        return;
    }
    var num = parseInt(obj.val() || parseInt(obj.text()));
    var returnfee = discount(num, count_fee);
    var onum = isNaN(returnfee[0]) ? 0 : returnfee[0];
    var gnum = isNaN(returnfee[1]) ? 0 : returnfee[1];
    var fnum = isNaN(returnfee[2]) ? 0 : returnfee[2];
    var paynum = isNaN(num) ? 0 : num;
    var closest = obj.closest(".pay-panel");
    if (isNaN(num)) {
        fnum = 100;
    }
    if (gnum > 0) {
        closest.find(".discount").show();
    } else {
        closest.find(".discount").hide();
    }
    closest.find(".totel_number").html(onum + gnum);
    if (typeof _gameType != "undefined" && _gameType == 24) {
        fnum = (fnum / 10);
        onum = Math.floor(onum / 10);
        gnum = Math.floor(gnum / 10);
    }
    closest.find(".points_base").html(fnum / 100 * _GPConf.rate);

    onum = onum / 100 * _GPConf.rate;
    gnum = Math.ceil(gnum / 100 * _GPConf.rate);
    if (window._GPConf) {
        closest.find(".points_upgrade").html(onum + " " + _GPConf.name);
        closest.find(".points_donate").html(gnum + " " + _GPConf.name);
    } else {
        closest.find(".points_upgrade").html(onum);
        closest.find(".points_donate").html(gnum);
    }
    closest.find(".label-option span").html(paynum);
}

var eventOfInputMoney = function(item, otherfunction) {
    $this = $(item);
    $this.keyup(function() {
        if (typeof otherfunction === "function") {
            otherfunction();
        }

    });
    if (typeof otherfunction === "function") {
        otherfunction();
    }
}

var eventOfClickMoney = function(item, otherfunction) {
    $this = $(item);
    $this.click(function() {
        if (typeof otherfunction === "function") {
            otherfunction();
        }

    });
    if (typeof otherfunction === "function") {
        otherfunction();
    }
}
$(".alipay-tips").hover(function() {
    $(this).addClass("shown");
}, function() {
    $(this).removeClass("shown");
});